package com.tienon.message.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import com.tienon.message.entity.Push_App;
import com.tienon.message.mapper.Push_AppMapper;
import com.tienon.message.mapper.Raw_msg_batchMapper;
import com.tienon.message.util.SpringUtil;

public class InsertAppMsgs {

	public static void main(String[] args) throws ParseException {

		InsertAppMsgs insertApp = new InsertAppMsgs();
		insertApp.insertDatas();
		
	}
	private Push_AppMapper appDao;
	private Raw_msg_batchMapper rawDao;
	
	public InsertAppMsgs(){
		appDao = SpringUtil.getBean(Push_AppMapper.class);
		rawDao = SpringUtil.getBean(Raw_msg_batchMapper.class);
	}
	
	public void insertDatas() throws ParseException {

		Random rand = new Random();

		int i = 0;
		int batId = 0;

		while (i < 200) {
			Push_App data = new Push_App();

			int mod = rand.nextInt(100) +1;

			if (i % mod == 0) {
				batId = rawDao.getKey();
			}
			
			
			data.setBatId(batId);
			data.setSerial(rawDao.getKey());
			data.setSplitFlag(rand.nextInt(2));
			data.setFilter(0);
			data.setOccDate(randomDate1());

			data.setMsgType(rand.nextInt(5));
			data.setSendNow(0);
			data.setSendWay(rand.nextInt(3) + 1);
			data.setBsnCd(UUID.randomUUID().toString().substring(0, 5));
			data.setSendTimes(0);
			data.setFeeType(rand.nextInt(2));
			data.setOutChannle("1");
			data.setPrior(rand.nextInt(11));
			data.setReportFlag(rand.nextInt(2));
			data.setLinkFlag(rand.nextInt(2));
			data.setState(1);
			data.setOutChanCustomId(UUID.randomUUID().toString()
					.substring(0, 8));

			if (data.getSplitFlag() == 1) {
				int total = rand.nextInt(10);
				data.setSplitTotal(total);
				while (total > 0) {
					data.setSplitId(total);

					data.setFee(rand.nextInt(5));
					data.setSendTime(randomDate2());
					data.setMsg(UUID.randomUUID().toString());
					data.setOccTime(randomTime1());
					appDao.insertSelective(data);

					total--;
				}
			} else {
				data.setSplitId(1);
				data.setSplitTotal(1);
				data.setFee(rand.nextInt(5));
				data.setSendTime(randomDate2());
				data.setMsg(UUID.randomUUID().toString());
				data.setOccTime(randomTime1());
				appDao.insertSelective(data);

			}
			i++;
		}
		System.out.println(i);
		System.out.println("Over");

	}

	public Date randomDate1() throws ParseException {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = format.parse("2015-1-1 00:00:00");
		// System.out.println(start);
		Date end = format.parse("2016-1-1 00:00:00");
		long time = start.getTime() + (long) (start.getTime() - end.getTime());

		return new Date(time);
	}

	public Date randomTime1() throws ParseException {

		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		Date start = format.parse("00:00:00");
		// System.out.println(start);
		Date end = format.parse("23:59:00");
		long time = start.getTime() + (long) (start.getTime() - end.getTime());

		return new Date(time);
	}

	public Date randomDate2() throws ParseException {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = format.parse("2016-1-1 00:00:00");
		// System.out.println(start);
		Date end = format.parse("2016-10-10 00:00:00");
		long time = start.getTime() + (long) (start.getTime() - end.getTime());

		return new Date(time);
	}

}
